/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builder;

import javax.swing.JPanel;
import org.balisunrise.annotation.util.AnnotationsUtilitiesException;
import org.balisunrise.annotation.util.NameUtil;
import org.balisunrise.annotation.util.SearchUtilities;
import org.balisunrise.application.Application;
import org.balisunrise.builder.CommonFactory;
import org.balisunrise.builder.FactoryException;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.VisualAction;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.EntityFilter;
import org.balisunrise.common.EntityGrid;
import org.balisunrise.common.EntityList;
import org.balisunrise.common.EntityLoader;
import org.balisunrise.common.Search;
import org.balisunrise.swing.transaction.panel.SearchPanel;
import org.balisunrise.swing.transaction.panel.layout.SearchLayoutBuilder;

/**
 *
 * @author Glauco
 */
public class SearchBuilder implements org.balisunrise.builder.SearchBuilder {

    private static final String MESSAGE = "Erro ao construir uma Search.\n";

    public Search create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        try{
            SearchUtilities su = SearchUtilities.create(entityClass, alias);
            return create(su);
        }catch(AnnotationsUtilitiesException aue){
            throw new BuilderException(MESSAGE +
                    aue.getMessage(), aue.getCause());
        }
    }

    public Search create(SearchUtilities searchUtilities)
            throws BuilderException {

        if(searchUtilities == null){
            throw new BuilderException(MESSAGE +
                    "editorUtilities não pode ser nulo.");
        }

        //cria a busca
        org.balisunrise.swing.transaction.Search search =
                new org.balisunrise.swing.transaction.Search();

        // cria o EntityFilter
        EntityFilter<JPanel> ef = Application.get()
                .getVisualEntityManager()
                .getEntityFilterBuilder()
                .create(searchUtilities.filterUtilities);

        // cria o EntityGrid
        EntityGrid<JPanel> eg = Application.get()
                .getVisualEntityManager()
                .getEntityGridBuilder()
                .create(searchUtilities.gridUtilities);

        // Cria o Painel de Ações
        EntityActions<VisualAction,JPanel> ea = Application.get()
                .getVisualEntityManager()
                .getEntityActionBuilder()
                .create(searchUtilities.entityClass,
                        searchUtilities.alias);

        // atribui o pai
        ef.setOwner(search);
        eg.setOwner(search);
        ea.setOwner(search);

        // cria entityLoader
        EntityLoader el = null;
        try{
            CommonFactory bf = Application.get().getFactory();
            EntityList list = bf.createEntityList();
            list.setOwner(search);
            el = bf.createEntityLoader(searchUtilities.entityClass,list);
            el.setOwner(search);
        }catch(FactoryException fex){
            throw new BuilderException(MESSAGE +
                    fex.getMessage(),fex);
        }

        // atribui dados ao Search
        search.setAlias(searchUtilities.alias);
        search.setEntityClass(searchUtilities.entityClass);
        search.setTitle(searchUtilities.title);
        search.setEntityFilter(ef);
        search.setEntityGrid(eg);
        search.setEntityActions(ea);
        search.setEntityLoader(el);
        search.setVisualRepresentation(new SearchPanel(
                ef.getVisualRepresentation(),
                eg.getVisualRepresentation(),
                ea.getVisualRepresentation(),
                new SearchLayoutBuilder()));

        // nome do painel
        String name = NameUtil.buildName(search);
        search.getVisualRepresentation().setName(name);
        search.getVisualRepresentation().getAccessibleContext().setAccessibleName(name);
        search.setName(name);
        
        return search;
    }

}
